/*
  Doesn't draw zero lenght strings (use for place holders)
  start a line with a '_' to draw text with color <textColorDim>
  [example:
    Hilighted\n
    _dimmed 1\n
    _dimmed 2
  [/example]
*/

class Text extends Entity {

  final float textOutAlpha = 255;
  final float textInAlpha = 20;

  final float bgBoxAlpha = 150;

  final color textColor = color(63);
  final color textColorDim = color(86, 100);

  String[] rows;
  boolean isPopped, isMoving;

  float outX, outY, outZ;
  float inX, inY, inZ;

  float tweenX, tweenY, tweenZ, tweenLen;

  Text(String text) {
    this.rows = text.split("\n");
    isMoving = false;
    isPopped = false;
  }

  void popIn() {
    isPopped = false;
    isMoving = true;
  }

  void popUp() {
    isPopped = true;
    isMoving = true;
  }
  
  public void draw() {
    // skip empty strings
    if(rows.length == 1 && rows[0].length() < 1) return;
    
    float ratio = absz() / (TextCloud.popUpSize - TextCloud.popInSize);
    float alpha = ratio * (textOutAlpha - textInAlpha) + textInAlpha;

    float textSize = (absz() < 10 ? 10 : absz());
    float textHeight = textSize * rows.length + 5;

    textFont(bigFont, textSize);

    rectMode(CENTER);
    noStroke();

    // compensate for rounding errors
    if(ratio > 0.1)
      fill(backgroundColor, bgBoxAlpha * ratio);
    else
      fill(backgroundColor, 0);

    // apply a scaled margin to the background box
    rect(x(), y(), 
        width*2, ratio * (textHeight + 20));

    // go trough the text and draw each row
    for(int i = 0; i < rows.length; i++) {
      float tw = textWidth(rows[i]);
      String text = rows[i];

      if(rows[i].startsWith("_")) {
        fill(textColorDim, alpha);
        text = text.substring(1, text.length());
      }
      else
        fill(textColor, alpha);

      text(text, x(), y() + textSize * i - (rows.length * textSize)/2); 
    }
  }
}
